package cn.tedu.validation;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class ValidationController {
    @RequestMapping("/login")
        //BindingResult用于提示信息
    // BindingResult参数必须直接添加在被验证对象的右侧，它们中间不可以添加其它参数！
        public String login(@Valid User user, BindingResult bindingResult){
            System.out.println("user = " + user);
            if(bindingResult.hasErrors()){
              return bindingResult.getFieldError().getDefaultMessage();
            }else{
                return "ok";
            }

        }
}
